package com.whcoding.test.example;

import com.alibaba.fastjson.JSON;
import com.whcoding.test.example.crt.*;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.*;


public class KfptDemo {

    public static final String HMAC_SHA1 = "HmacSHA1";
    public static final char[] DIGITAL = "0123456789ABCDEF".toCharArray();

    /**
     * <pre>
     * 请求流程：
     *
     * 1、其他 API，cid + 私钥生成的签名 + 业务参数
     * </pre>
     */
    public static void main(String[] args) throws Exception {

        long start = System.currentTimeMillis();

        // URL 前缀
        String baseUrl = "https://openapi-test.sinoiov.cn/save/apis/";

        // 检索最新节点
        String vLastLocationV3Url = baseUrl + "routerPath";
        Map<String, String> vLastLocationParam = new HashMap<>(16);
        // 客户端 ID
        vLastLocationParam.put("cid", "");
        // 私钥
        vLastLocationParam.put("srt", "");
        // 业务参数
        vLastLocationParam.put("vclN", "陕UH0009");
//        vLastLocationParam.put("timeNearby", "24");
//        //开始时间(开始时间与结束时间相差72小时内)
        vLastLocationParam.put("qryBtm", "2022-12-21 15:56:46");
        //结束时间
        vLastLocationParam.put("qryEtm", "2022-12-22 15:56:46");
        //车牌颜色
        vLastLocationParam.put("vco", "2");

        String vLastLocationV3Result = httpsCall(vLastLocationV3Url, vLastLocationParam, false);

        // 处理业务服务返回
        System.out.println(vLastLocationV3Result);
        long finish = System.currentTimeMillis();
        long timeElapsed = finish - start;
        System.out.println("耗时" + timeElapsed / 1000 + "秒");
    }

    /**
     * https 调用，1、处理参数：①移除 srt，②计算签名，③增加 sign；2、将 map 参数拼接
     * 成 string 格式；3、发起调用；4、处理结果
     *
     * @param url    url
     * @param param  请求参数
     * @param strict 严格模式 true 校验证书等；false 不校验
     */
    public static String httpsCall(String url, Map<String, String> param, boolean strict) throws Exception {
        // 1、处理参数
        processParam(param);
        // 2、将 map 参数拼接成 string 格式
        String reqBody = convertMapToString(param);
        System.out.printf("Request - url: %s, reqBody: %s\n", url, reqBody);
        if (!strict) {
            // 信任证书
            trustAllCerts();
            // 信任域名
            trustAllHosts();
        }
        // 3、发起调用；
        HttpsURLConnection conn = (HttpsURLConnection) new URL(url).openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Length", String.valueOf(reqBody.getBytes(StandardCharsets.UTF_8).length));
        conn.setRequestProperty("charset", "UTF-8");
        conn.connect();

        PrintWriter writer = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8));
        writer.print(reqBody);
        writer.flush();
        writer.close();

        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
        String line;
        StringBuilder result = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            result.append(line);
        }

        reader.close();
        System.out.printf("Response - code: %s, message: %s, respBody: %s\n"
                , conn.getResponseCode(), conn.getResponseMessage(), result);
        // 4、处理结果
        if (StringUtils.isNotBlank(result.toString())) {
            //将数据 转为对象
            ResultVO resultVehicleTrackVO = JSON.parseObject(result.toString(), ResultVO.class);
            BaseResultVO baseResultVO = JSON.parseObject(result.toString(), BaseResultVO.class);

            System.out.println("序列号返回的结果为1：" + JSON.toJSON(resultVehicleTrackVO));
            System.out.println("序列号返回的结果为2：" + JSON.toJSON(baseResultVO));
        }
        return result.toString();
    }


    /**
     * 保存轨迹数据
     */
    private static void saveResultData(ResultVO resultVO) {

        if (Objects.nonNull(resultVO.getResult())) {

            ResultVehicleTrackVO resultVehicleTrackVO = resultVO.getResult();

            //轨迹数据
            List<VehicleTrackVO> trackVOS = resultVehicleTrackVO.getTrackArray();

            //停车数据
            List<VehicleTrackParkVO> parkVOS = resultVehicleTrackVO.getParkArray();

        }

    }


    /**
     * 处理参数：①移除 srt，②计算签名，③增加 sign；
     *
     * @param param 原始参数
     */
    private static void processParam(Map<String, String> param) throws Exception {
        if (!param.containsKey("srt")) {
            throw new NullPointerException();
        }
        String srt = param.remove("srt");
        List<String> paramValueList = new ArrayList<>();
        for (Map.Entry<String, String> entry : param.entrySet()) {
            paramValueList.add(entry.getKey() + entry.getValue());
        }
        Collections.sort(paramValueList);
        String[] data = new String[paramValueList.size()];
        paramValueList.toArray(data);
        byte[] signature = hmacSha1(data, srt.getBytes(StandardCharsets.UTF_8));
        String sign = encodeHexStr(signature);
        param.put("sign", sign);
    }


    /**
     * 将参数转换为字符串
     *
     * @param param
     * @return
     */
    private static String convertMapToString(Map<String, String> param) {
        StringBuilder sb = new StringBuilder();
        if (param != null && !param.isEmpty()) {
            for (Map.Entry<String, String> entry : param.entrySet()) {
                sb.append(entry.getKey()).append("=").append(entry.getValue
                        ()).append("&");
            }
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }


    /**
     * 信任所有证书
     */
    private static void trustAllCerts() throws Exception {
        TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        }};


        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()
        );
    }


    /**
     * 信任所有域名
     */
    private static void trustAllHosts() {
        HostnameVerifier trustAllHosts = (hostname, session) -> true;
        HttpsURLConnection.setDefaultHostnameVerifier(trustAllHosts);
    }

    /**
     * HmacSha1 定制算法
     *
     * @param strings 内容
     * @param key     key
     * @return 签名
     */
    private static byte[] hmacSha1(String[] strings, byte[] key) throws Exception {
        SecretKeySpec signingKey = new SecretKeySpec(key, HMAC_SHA1);
        Mac mac = Mac.getInstance(HMAC_SHA1);
        mac.init(signingKey);
        for (String data : strings) {
            mac.update(data.getBytes(StandardCharsets.UTF_8));
        }
        return mac.doFinal();
    }

    /**
     * 字节转为 Hex 字符串
     *
     * @param bytes 字节数组
     * @return Hex 字符串
     */
    private static String encodeHexStr(final byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        char[] result = new char[bytes.length * 2];
        for (int i = 0; i < bytes.length; i++) {
            result[i * 2] = DIGITAL[(bytes[i] & 0xf0) >> 4];
            result[i * 2 + 1] = DIGITAL[bytes[i] & 0x0f];
        }
        return new String(result);
    }


}